﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using Lbb.Localization;
using Lbb.URating.DL;
using Lbb.URating.Localization;
using Lbb.Data;

namespace HU8.UCenter.Plugins.URating.DAL
{
    /// <summary>
    /// 评分项操作库
    /// </summary>
    /// <author>Chiron</author>
    public class U_ClickRatingRepository : RepositoryBase<U_ClickRating>
    {
        #region Ctors
        /// <summary>
        /// 构造评分项操作库
        /// </summary>
        /// <param name="db"></param>
        public U_ClickRatingRepository(URatingDataContext db)
            : base(db)
        {
        }
        #endregion Ctors

        #region Get
        /// <summary>
        /// 取得评分项
        /// </summary>
        /// <param name="id">评分项ID</param>
        /// <returns></returns>
        public U_ClickRating Get(Guid? id)
        {
            return id.HasValue && id != Guid.Empty ? Get(d => d.RatingID == id) : null;
        }
        #endregion Get

        #region Find
        /// <summary>
        /// 搜索评分项
        /// </summary>
        /// <param name="keyword">关键字</param>
        /// <param name="categoryIds">评分分类</param>
        /// <param name="clickIds">点击器</param>
        /// <param name="objIds">关联对象</param>
        /// <param name="minClickCount">点击量下限</param>
        /// <param name="maxClickCount">点击量上限</param>
        /// <param name="minAverageRating">平均分下限</param>
        /// <param name="maxAverageRating">平均分上限</param>
        /// <returns></returns>
        public IQueryable<U_ClickRating> Find(string keyword = null, IEnumerable<Guid> categoryIds = null, IEnumerable<Guid> clickIds = null, IEnumerable<string> objIds = null,
            int? minClickCount = null, int? maxClickCount = null, decimal? minAverageRating = null, decimal? maxAverageRating = null)
        {
            var data = GetAll();

            if (!string.IsNullOrEmpty(keyword)) data = data.Where(d => d.ObjectID.Contains(keyword) || d.U_Click.Title.Contains(keyword) || d.U_Click.U_ClickCategory.Title.Contains(keyword));

            if (clickIds != null && (clickIds = clickIds.Where(d => d != Guid.Empty).ToList()).Count() > 0)
            {
                data = data.Where(d => clickIds.Contains(d.ClickID));
            }
            else
            {
                if (categoryIds != null && (categoryIds = categoryIds.Where(d => d != Guid.Empty).ToList()).Count() > 0) data = data.Where(d => categoryIds.Contains(d.U_Click.CategoryID));
            }

            if (objIds != null && (objIds = objIds.Where(d => !string.IsNullOrEmpty(d)).ToList()).Count() > 0) data = data.Where(d => objIds.Contains(d.ObjectID));

            if (minClickCount.HasValue) data = data.Where(d => d.ClickCount >= minClickCount.Value);

            if (maxClickCount.HasValue) data = data.Where(d => d.ClickCount <= maxClickCount.Value);

            if (minAverageRating.HasValue) data = data.Where(d => d.AverageRating >= minAverageRating.Value);

            if (maxAverageRating.HasValue) data = data.Where(d => d.AverageRating <= maxAverageRating.Value);

            return data;
        }
        #endregion Find

        #region Override
        /// <summary>
        /// 添加评分项
        /// </summary>
        /// <param name="obj">评分项</param>
        public override void Add(U_ClickRating obj)
        {
            if (obj != null && obj.RatingID == Guid.Empty) obj.RatingID = Guid.NewGuid();
            base.Add(obj);
        }
        /// <summary>
        /// 更新评分项
        /// </summary>
        /// <param name="obj">评分项</param>
        public override void Update(U_ClickRating obj)
        {
            CheckArg(obj);
            var old = Get(obj.RatingID);
            if (old == null) throw new ArgumentNullException(string.Format(OperatorRes.NotFindEditingObj, U_ClickRatingRes.ClassName));

            try
            {
                Table.Attach(obj, old);
            }
            catch (InvalidOperationException)
            {
                DB.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
            }
        }
        #endregion Override

    }
}